<!--
 * @Description: 我要报修
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:13:16
-->
<template>
  <div class="repairs-container">
    <h2>工程设备</h2>

    <div class="form-group">
      <label for="equipment">Select</label>

      <el-cascader v-model="value" :options="equipmentList" @change="onEquipmentChange"></el-cascader>

    </div>

    <div class="form-group">
      <label for="reason">报修原因</label>
      <textarea id="reason" v-model="repairReason" class="form-control" rows="1" placeholder="请输入报修原因..."></textarea>
    </div>

    <button class="submit-btn" @click="submitRepair" :disabled="!isFormValid">
      提交
    </button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'Repairs',
  data() {
    return {
      selectedEquipment: '',
      repairReason: '',
      equipmentList: [], // 存储从接口获取的设备列表
      list: [], // 本地数据作为后备

    };
  },
  computed: {
    isFormValid() {
      return this.selectedEquipment && this.repairReason.trim();
    }
  },
  created() {
    this.fetchEquipmentList();
  },
  methods: {
    async fetchEquipmentList() {
      try {
        // 替换为实际的Swagger接口地址
        const response = await axios.get('http://101.37.38.67:7001/dashboard/findEngineerDeviceTree')
        console.log('设备列表:', response.data.data);
        this.equipmentList = response.data.data.map((item) => ({
          value: item.id.toString(),
          label: item.name,
          children: item.children.map(child => ({
            value: child.id.toString(),
            label: child.name
          }))
        })) || [];
        this.list = response.data.data
        console.log('Formatted equipment list:', this.equipmentList);
      } catch (error) {
        console.error('获取设备列表失败:', error);
        // 如果接口失败，使用本地数据作为后备
        this.equipmentList = this.devices.map((device, index) => ({
          id: index + 1,
          name: device
        }));
      }
    },
    onEquipmentChange(values) {
      console.log('Selected equipment:', values);
    },
    async submitRepair() {
      if (!this.isFormValid) return;

      try {
        // 替换为实际的报修接口地址
        const response = await axios.post('/workorder/repairOrder', {
          equipmentId: this.selectedEquipment,
          reason: this.repairReason
        });

        if (response.data.success) {
          alert('报修提交成功！');
          this.resetForm();
        } else {
          alert('报修提交失败: ' + response.data.message);
        }
      } catch (error) {
        console.error('报修提交失败:', error);
        alert('报修提交失败，请稍后重试');
      }
    },
    resetForm() {
      this.selectedEquipment = '';
      this.repairReason = '';
    }
  }
};
</script>

<style scoped>
.repairs-container {
  max-width: 1500px;
  margin: 0 auto;
  padding: 5px;
  font-family: Arial, sans-serif;
}

h2 {
  text-align: left;
  margin-bottom: 20px;
  color: #4a4a4a;
}

.form-group {
  margin-bottom: 10px;
}

label {
  display: block;
  margin-bottom: 6px;
  font-weight: bold;
  color: #6c6a6a;
}

.form-control {
  width: 100%;
  padding: 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

select.form-control {
  height: 40px;
}

textarea.form-control {
  min-height: 120px;
  resize: vertical;
}

.submit-btn {
  display: block;
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.submit-btn:hover {
  background-color: #45a049;
}

.submit-btn:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}
</style>